package com.tvunetworks.device.extra.service.impl;

import com.tvunetworks.device.extra.constants.DeviceExtraConstants;
import com.tvunetworks.device.extra.model.BucketInfo;
import com.tvunetworks.device.extra.model.param.ChooseRegionParam;
import com.tvunetworks.device.extra.service.HttpService;
import com.tvunetworks.device.extra.service.RegionService;
import com.tvunetworks.device.extra.util.HttpRequestUtilV2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
 * @author liam
 */

@Service
@Slf4j
public class RegionServiceImpl implements RegionService {

    @Autowired
    private HttpService httpService;

    @Override
    public BucketInfo defaultRegionByIp(HttpServletRequest request, @RequestParam String imageType) {

        String ip = HttpRequestUtilV2.getClientPublicIP(request);
        ChooseRegionParam param = new ChooseRegionParam()
                .setImageType(imageType)
                .setRequestIp(ip)
                .setType(DeviceExtraConstants.ChooseRegion.IP_TYPE);

        return httpService.chooseRegion(param);
    }
}
